package cn.cool.middleware.biz_exception_retry.config;


import cn.cool.middleware.biz_exception_retry.aspect.BizExceptionRetryAspect;
import cn.cool.middleware.biz_exception_retry.config.processor.BizExceptionRetryObjectBeanPostProcessor;
import cn.cool.middleware.biz_exception_retry.config.registry.BeanMethodRegistry;
import cn.cool.middleware.biz_exception_retry.domain.IBizExceptionRetryModuleService;
import cn.cool.middleware.biz_exception_retry.domain.IBizExceptionRetryRecordService;
import cn.cool.middleware.biz_exception_retry.domain.IBizExceptionRetryTaskService;
import cn.cool.middleware.biz_exception_retry.domain.adapter.repository.IBizExceptionRetryRepository;
import cn.cool.middleware.biz_exception_retry.domain.service.BizExceptionRetryModuleService;
import cn.cool.middleware.biz_exception_retry.domain.service.BizExceptionRetryRecordService;
import cn.cool.middleware.biz_exception_retry.domain.service.BizExceptionRetryTaskService;
import cn.cool.middleware.biz_exception_retry.infra.adapter.repository.BizExceptionRetryRepository;
import cn.cool.middleware.biz_exception_retry.trigger.listener.BizExceptionRetryJob;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;


@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnExpression("${biz.exception.retry.enable:false}")
@Import(MyBatisConfiguration.class)
public class BizExceptionRetryAutoConfiguration {

    @Bean
    public BizExceptionRetryAspect bizExceptionRetryAspect() {
        return new BizExceptionRetryAspect();
    }

    @Bean
    public BizExceptionRetryConfig bizExceptionRetryConfig() {
        return new BizExceptionRetryConfig();
    }

    @Bean
    public IBizExceptionRetryRecordService bizExceptionRetryRecordService() {
        return new BizExceptionRetryRecordService();
    }

    @Bean
    public IBizExceptionRetryTaskService bizExceptionRetryTaskService() {
        return new BizExceptionRetryTaskService();
    }

    @Bean
    public IBizExceptionRetryModuleService bizExceptionRetryModuleService() {
        return new BizExceptionRetryModuleService();
    }

    /**
     * 业务异常重试 record 操作
     */
    @Bean
    public IBizExceptionRetryRepository bizExceptionRetryRepository() {
        return new BizExceptionRetryRepository();
    }

    @Bean
    public BeanMethodRegistry beanMethodRegistry() {
        return new BeanMethodRegistry();
    }

    @Bean
    public BizExceptionRetryObjectBeanPostProcessor bizExceptionRetryObjectBeanPostProcessor(BeanMethodRegistry beanMethodRegistry) {
        return new BizExceptionRetryObjectBeanPostProcessor(beanMethodRegistry);
    }

    @Bean
    @ConditionalOnExpression("${biz.exception.retry.job.enable:true}")     // 是否开启业务异常重试
    public BizExceptionRetryJob bizExceptionRetryJob() {
        return new BizExceptionRetryJob();
    }

}
